import uvicorn
from fastapi import FastAPI, Path, Query
from fastapi.openapi.docs import get_swagger_ui_html
from starlette.staticfiles import StaticFiles

# 创建 FastAPI 应用实例，并把默认的/docs请求禁用
# debug表示是否开启调试模式，默认是False
app = FastAPI(docs_url=None,debug=True)
app.mount("/static", StaticFiles(directory="../static"), name="static")    # 静态文件

@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )




@app.get("/users/{user_id}")
async def get_user(user_id: int = Path(...,         # ... 省略号，它是一个特殊的对象，表示参数是必填的
                                       title="用户ID",    # 参数的标题
                                       ge=0,             # 最小值是1
                                       le=1000,          # 最大值是1000
                                       )):
    return {"user_id": user_id}

@app.get("/books/{book_name}")
async def get_user(book_name: str = Path(...,         # ... 省略号，它是一个特殊的对象，表示参数是必填的
                                       title="book name",    # 参数的标题
                                       min_length=3,         # 字符串的最小长度是3
                                       max_length=10,        # 字符串的最大长度是10
                                       )):
    return {"book_name": book_name}

@app.get("/items/{item_name}")
async def get_item(item_name: str = Path(
    ...,
    title="Item name",
    pattern='^[a|b|c]-\\d*$'
)):
    return {"item_name": item_name}


@app.get("/users")
async def get_user(page_index: int = Query(1,           # 这里是Query可以指定一个默认值，也可像Path一指定为...表法必填
                                           alias="page-index", # 别名
                                           title="页码",       # 参数标题
                                           ge=1,              # 指定参数的最小值
                                           le=1000            # 指定参数的最大值
                                           )):
    return {"page_index": page_index}



if __name__ == '__main__':
    uvicorn.run("main:app", host="127.0.0.1", port=8004, reload=True)